<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="Janmas">



    <meta name="description" content="Janmas！一个又菜又爱玩的程序猿。不停的在立Flag，又一直在凭实力打着自己的脸。">


    <meta name="keywords" content="Janmas,Blog,墨鱼">


<title>
    Centos安装Nginx |
    
    Janmas</title>



    <link rel="icon" href="/favicon.ico">



    <!-- stylesheets list from _config.yml -->
    
        <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
        <script src="/js/script.js"></script>
    
        <script src="/js/tocbot.min.js"></script>
    



    
    
        
    



    <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
    <script>
        if (window.mermaid) {
            mermaid.initialize({theme: 'forest'});
        }
    </script>


<meta name="generator" content="Hexo 5.4.1"></head>

<body>
    <script>
        // this function is used to check current theme before page loaded.
        (() => {
            const currentTheme = window.localStorage && window.localStorage.getItem('theme') || '';
            const isDark = currentTheme === 'dark';
            const pagebody = document.getElementsByTagName('body')[0]
            if (isDark) {
                pagebody.classList.add('dark-theme');
                // mobile
                document.getElementById("mobile-toggle-theme").innerText = "· Dark"
            } else {
                pagebody.classList.remove('dark-theme');
                // mobile
                document.getElementById("mobile-toggle-theme").innerText = "· Light"
            }
        })();
    </script>

    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">Janmas&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">Janmas&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
            <div class="main">
                <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">Centos安装Nginx</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">Janmas</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">January 10, 2022&nbsp;&nbsp;14:31:20</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/Centos/">Centos</a>
                            
                                <a href="/categories/Centos/Nginx/">Nginx</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <h2 id="安装nginx的准备工作"><a href="#安装nginx的准备工作" class="headerlink" title="安装nginx的准备工作"></a>安装nginx的准备工作</h2><p>系统信息:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CentOS Linux release 7.9.2009 (Core)</span><br></pre></td></tr></table></figure>

<p>安装编译环境:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># yum -y install gcc gcc-c++</span><br></pre></td></tr></table></figure>

<p>安装pcre软件包（使nginx支持http rewrite模块）:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># yum install -y pcre pcre-devel</span><br></pre></td></tr></table></figure>

<p>安装 openssl-devel（使 nginx 支持 ssl）:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># yum install -y openssl openssl-devel </span><br></pre></td></tr></table></figure>

<p>安装zlib和Gd:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># yum install -y zlib zlib-devel gd gd-devel</span><br></pre></td></tr></table></figure>

<p>创建nginx用户:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># useradd -s /sbin/nologin nginx </span><br></pre></td></tr></table></figure>



<h2 id="编译安装nginx"><a href="#编译安装nginx" class="headerlink" title="编译安装nginx"></a>编译安装nginx</h2><p>下载安装包并解压:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># wget https://nginx.org/download/nginx-1.21.4.tar.gz</span><br><span class="line"># tar -zxvf ./nginx-1.21.4.tar.gz</span><br><span class="line"># cd nginx-1.21.4/</span><br></pre></td></tr></table></figure>

<p>编辑配置:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"># ./configure --prefix=/usr/local/nginx \</span><br><span class="line">--user=nginx \</span><br><span class="line">--group=nginx \</span><br><span class="line">--with-pcre \</span><br><span class="line">--with-http_ssl_module \</span><br><span class="line">--with-http_v2_module \</span><br><span class="line">--with-http_realip_module \</span><br><span class="line">--with-http_addition_module \</span><br><span class="line">--with-http_sub_module \</span><br><span class="line">--with-http_dav_module \</span><br><span class="line">--with-http_flv_module \</span><br><span class="line">--with-http_mp4_module \</span><br><span class="line">--with-http_gunzip_module \</span><br><span class="line">--with-http_gzip_static_module \</span><br><span class="line">--with-http_random_index_module \</span><br><span class="line">--with-http_secure_link_module \</span><br><span class="line">--with-http_stub_status_module \</span><br><span class="line">--with-http_auth_request_module \</span><br><span class="line">--with-http_image_filter_module \</span><br><span class="line">--with-http_slice_module \</span><br><span class="line">--with-mail \</span><br><span class="line">--with-threads \</span><br><span class="line">--with-file-aio \</span><br><span class="line">--with-stream \</span><br><span class="line">--with-mail_ssl_module \</span><br><span class="line">--with-stream_ssl_module </span><br><span class="line"></span><br><span class="line"># make &amp;&amp; make install</span><br></pre></td></tr></table></figure>

<p><code>./configure --help 可以查看参数及参数描述</code></p>
<h2 id="把nginx加入系统服务"><a href="#把nginx加入系统服务" class="headerlink" title="把nginx加入系统服务"></a>把nginx加入系统服务</h2><p><code>在/usr/lib/systemd/system/目录下创建名为nginx.service的文件  把以下内容写入nginx.service文件</code></p>
<p>编辑服务文件:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">[Unit]</span><br><span class="line">Description=nginx</span><br><span class="line">After=network.target remote-fs.target nss-lookup.target</span><br><span class="line"></span><br><span class="line">[Service]</span><br><span class="line">Type=forking</span><br><span class="line">PIDFILE=/usr/local/nginx/logs/nginx.pid (你nginx的pid文件路径)</span><br><span class="line">ExecStartPost=bin.sleep 0.1</span><br><span class="line">ExecStartPre=/usr/local/nginx/sbin/nginx(你nginx的可执行文件路径) -t -c /usr/local/nginx/conf/nginx.conf(你nginx的配置文件)</span><br><span class="line">ExecStart=/usr/local/nginx/sbin/nginx(同上) -c /usr/local/nginx/conf/nginx.conf(同上)</span><br><span class="line">ExecReload=/bin/kill -s HUP $MAINPID</span><br><span class="line">ExecStop=/bin/kill -s QUIT $MAINPID</span><br><span class="line">PrivateTmp=true</span><br><span class="line"></span><br><span class="line">[Install]</span><br><span class="line">WanteBy=multi-user.target</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>服务命令:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># systemctl start nginx.service (启动)</span><br><span class="line"># systemctl stop nginx.service  (停止)</span><br><span class="line"># systemctl reload nginx.service (重启)</span><br><span class="line"># systemctl status nginx.service (状态查看)</span><br></pre></td></tr></table></figure>



<h4 id="把nginx配置为全局命令"><a href="#把nginx配置为全局命令" class="headerlink" title="把nginx配置为全局命令"></a>把nginx配置为全局命令</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># ln -s /usr/local/nginx/sbin/nginx(你nginx的可执行文件路径) /usr/bin/nginx</span><br><span class="line"># nginx -v</span><br></pre></td></tr></table></figure>

<h4 id="nginx设置为开机自启"><a href="#nginx设置为开机自启" class="headerlink" title="nginx设置为开机自启"></a>nginx设置为开机自启</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl enable nginx.service</span><br></pre></td></tr></table></figure>

<h4 id="nginx简单命令"><a href="#nginx简单命令" class="headerlink" title="nginx简单命令"></a>nginx简单命令</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">nginx -c /path/to/nginx.conf     # 以特定目录下的配置文件启动nginx:</span><br><span class="line">nginx -s reload                  # 修改配置后重新加载生效</span><br><span class="line">nginx -s reopen                  # 重新打开日志文件</span><br><span class="line">nginx -s stop                    # 快速停止nginx</span><br><span class="line">nginx -s quit                    # 完整有序的停止nginx</span><br><span class="line">nginx -t                         # 测试当前配置文件是否正确</span><br><span class="line">nginx -t -c /path/to/nginx.conf  # 测试特定的nginx配置文件是否正确</span><br></pre></td></tr></table></figure>
        </div>

        
            <section class="post-copyright">
                
                
                
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/Centos/"># Centos</a>
                    
                        <a href="/tags/Nginx/"># Nginx</a>
                    
                        <a href="/tags/Nginx%E5%AE%89%E8%A3%85/"># Nginx安装</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2022/01/10/Centos%E5%AE%89%E8%A3%85FFmpeg/">Centos安装FFmpeg</a>
            
            
        </section>


    </article>
</div>

            </div>
            <footer id="footer" class="footer">
    <div class="copyright">
        <span>© Janmas | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>



    </div>
</body>

</html>